var mongoose = require('mongoose');

var date = new Date();
var PostSchema = new mongoose.Schema({
	title : String,
	decription : String,
	intor : String,
	pv: {
		type : Number,
		default : 0
	},
	meta:{
		createAt : {
			type : Number,
			default : date.getTime()
		},
		updateAt:{
			type : Number,
			default : date.getTime()
		}
	}
})

PostSchema.pre('save', function(next){
	if ( this.isNew ) {
		this.meta.createAt = this.meta.updateAt = Date.now();
	}else{
		this.meta.updateAt = Date.now()
	}
	next();
})

PostSchema.statics = {
	fetch:function(cb){
		return this.find({}).exec(cb);
	},
	findById: function(id, cb){
		return this.findOne({_id:id}).exec(cb);
	}
}

module.exports= PostSchema